from django.db.models.signals import pre_delete, post_save
from product.models import Category, Product
from satchmo_store.contact.forms import ContactInfoForm
from satchmo_store.contact.models import Contact, AddressBook
from signals_ahoy.signals import form_init, form_postsave
from satchmo_store.shop.models import Order
from satchmo_tryton.listeners import (
    on_delete_obj_mapper, on_save_obj_mapper, tryton_contact_add_form_fields,
    tryton_contact_form_postsave)
from satchmo_tryton.models import LocalContact


# Signal registering.
def _reg_factory(signal, method):
    """Factory which created a registration utility method"""

    def _method(model):
        signal.connect(method, sender=model)

    return _method


# a tuple of all objects which need meta-data
_objmap_models = (
    AddressBook,
    Category,
    Contact,
    LocalContact,
    Product,
    Order,
    )

map(_reg_factory(pre_delete, on_delete_obj_mapper), _objmap_models)
map(_reg_factory(post_save, on_save_obj_mapper), _objmap_models)
form_postsave.connect(tryton_contact_form_postsave, sender=ContactInfoForm)
form_init.connect(tryton_contact_add_form_fields, sender=None)
